package astar;

import java.util.LinkedList;

import domain.Board;
import domain.SquareWall;
import domain.Game.action;

public class CreateGraph {
	private Board _board;
	private int _graphSize;
	private WeightedGraph _weightedGraph;
	private int location=0; //this variable will be used to represent the location row column as a serial
							//number over the board
	
	public CreateGraph(Board board,int graphSize){
		this._board=board;
		this._graphSize=graphSize;
		this._weightedGraph=new WeightedGraph(this._graphSize);
	}
	
	
	public LinkedList<action> findShortestDistanceToFlag(int source){
		LinkedList<action> ans=new LinkedList<action>();
		//CREATING VERTIX IN GRAPH
		createVertics();
		//CREATING EDGES IN GRAPH
		createEdges();
		//Running Dijkstra
		//TODO: change following line return statement and not printing
		System.out.println("PRINTING GRAPH!!");
		this._weightedGraph.print();
		System.out.println("END OF PRINTING GRAPH");
	      final int [] pred = Dijkstra.dijkstra (this._weightedGraph, source);
	      for (int n=0; n<_graphSize; n++) {
	    	  Dijkstra.printPath (this._weightedGraph, pred, 0, n);
	      }
	      System.out.println("***Printing pred***");
	      for (int i = 0; i < pred.length; i++) {
			System.out.println("Printing pred: "+pred[i]);
		}
		
		return ans;
		
	}

	private void createEdges() {
		for (int row = 1; row < _board.getNumberOfRows()-1; row++) {
			for (int column = 1; column < _board.getNumberOfColumns()-1; column++) {
				if(!(_board.getSquare(row,column) instanceof SquareWall)){
					//creating north edge
					if(!(_board.getSquare(row-1,column) instanceof SquareWall)){
						_weightedGraph.addEdge(converterFromXYtoNum(row, column), converterFromXYtoNum(row-1,column), 1);
					}
					//creating east edge
					if(!(_board.getSquare(row,column+1) instanceof SquareWall)){
						_weightedGraph.addEdge(converterFromXYtoNum(row, column), converterFromXYtoNum(row, column+1), 1);
					}
					//creating south edge
					if(!(_board.getSquare(row+1,column) instanceof SquareWall)){
						_weightedGraph.addEdge(converterFromXYtoNum(row, column), converterFromXYtoNum(row+1,column), 1);
					}
					//creating west edge
					if(!(_board.getSquare(row,column-1) instanceof SquareWall)){
						_weightedGraph.addEdge(converterFromXYtoNum(row, column), converterFromXYtoNum(row,column-1), 1);
					}
				}
			}
		}
	}//end of createEdges


	private void createVertics() {
		for (int row = 1; row < _board.getNumberOfRows()-1; row++) {
			for (int column = 1; column < _board.getNumberOfColumns()-1; column++) {
				//Checking if the row, column location is a wall
				if(!(_board.getSquare(row,column) instanceof SquareWall)){
					//inserting the location to the graph
					location=converterFromXYtoNum(row, column);
					_weightedGraph.setLabel(location,"v"+location);
				}
			}
		}
	}
	
	public int converterFromXYtoNum(int x,int y){
		return y+x*_board.getNumberOfColumns();
	}
	
	public int getRowFromNum(int num){
		return num/_board.getNumberOfColumns();
	}
	
	public int getColumnFromNum(int num){
		return num%_board.getNumberOfColumns();
	}
	
	
//	public 

}
